'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-10-29 09:54:08
'''
#
# @lc app=leetcode.cn id=129 lang=python3
#
# [129] 求根到叶子节点数字之和
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sumNumbers(self, root: TreeNode) -> int:
        tmp = [0]
        ans = []
        def recurse(root):
            if not root:
                return
            if not root.left and not root.right:
                tmp[0]*=10
                tmp[0]+=root.val
                ans.append(tmp[0])
                tmp[0]//=10
                return
            tmp[0]*=10
            tmp[0]+=root.val
            recurse(root.left)
            recurse(root.right)
            tmp[0]//=10
        recurse(root)
        return sum(ans)
# @lc code=end

